<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="柜子型号名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入柜子型号名称"
          clearable
        />
      </el-form-item>
      <el-form-item label="冷热类型" prop="coldHotType">
        <el-select v-model="queryParams.coldHotType" filterable placeholder="请选择">
          <el-option
            v-for="item in container_type"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="柜门数量" prop="doorCount">
        <el-select 
          v-model="queryParams.doorCount" 
          placeholder="请选择柜门数量" 
          clearable
          style="width:240px"
        >
          <el-option 
          v-for="item in container_door_count" 
          :key="item.value" 
          :label="item.label"
          :value="item.value" 
          />
        </el-select>
      </el-form-item>
      <el-form-item label="型号名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入型号名称"
          clearable
        />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="queryParams.remark"
          placeholder="请输入备注"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['produce:produce-plan:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['produce:produce-plan:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['produce:produce-plan:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getContainerTypeList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="containerTypelList" @selection-change="handleSelectionChange" :default-sort = "{prop: 'id', order: 'descending'}">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column sortable label="id" align="center" prop="id" :show-overflow-tooltip="true" />
      <el-table-column label="柜子型号名称" align="center" prop="name" :show-overflow-tooltip="true" />
      <el-table-column label="冷热类型" align="center" prop="coldHotType" :show-overflow-tooltip="true" >
        <template slot-scope="scope">
          {{ parseInt(scope.row.coldHotType) === 0 ? '冷柜' : '热柜' }}
        </template>
      </el-table-column>
      <el-table-column label="柜门数量" align="center" prop="doorCount" :show-overflow-tooltip="true" />
      <el-table-column label="陈列容量(瓶)" align="center" prop="displayVolume" :show-overflow-tooltip="true" />
      <el-table-column label="总容量(L)" align="center" prop="cabinetVolume" :show-overflow-tooltip="true" />
      <el-table-column label="层数" align="center" prop="layerCount" :show-overflow-tooltip="true" />
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
      <!-- <el-table-column label="创建时间" align="center" prop="createTime" :show-overflow-tooltip="true" >
        <template slot-scope="scope">
          {{ parseTime(new Date(scope.row.createTime)) }}
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getContainerTypeList"
    />

    <!-- 添加或修改冰柜类型对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="冰柜类型名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入" maxlength="20" show-word-limit/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="冷热类型" prop="coldHotType">
              <el-select v-model="form.coldHotType" filterable placeholder="请选择" style="width:240px">
                <el-option
                  v-for="item in container_type"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="陈列容量(瓶)" prop="displayVolume">
              <el-input v-model="form.displayVolume" placeholder="请输入容量" type="number" oninput="if(value.length > 3)value =value.slice(0,3)"> 
                <i slot="suffix"  style="font-style:normal;margin-right: 10px; line-height: 30px; color: #1e1e1e">瓶</i>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="最多层数" prop="layerCount">
              <el-input v-model="form.layerCount" placeholder="请输入层数" maxlength="1" show-word-limit/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="黄金位层数" prop="goldenTier" v-show="form.layerCount">
            <div>
              <el-checkbox-group v-model="goldenTier" @change="handleGoldenLayerChange">
                  <el-checkbox v-for="item in goldLayerList" :key="item.label" :label="item.label" ></el-checkbox>
              </el-checkbox-group>
            </div>
          </el-form-item>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="柜门数量" prop="doorCount">
              <el-select 
                v-model="form.doorCount" 
                placeholder="请选择柜门数量" 
                clearable
                style="width:240px"
              >
                <el-option 
                v-for="item in container_door_count" 
                :key="item.value" 
                :label="item.label"
                :value="item.value" 
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="柜门方向" prop="doorDirectionLeft" v-if="form.doorCount === 1">
              <el-select 
                  v-model="form.doorDirectionLeft" 
                  placeholder="请选择开门方向" 
                  clearable
                  style="width:240px"
                >
                  <el-option 
                  v-for="item in container_openDoor_direction" 
                  :key="item.value" 
                  :label="item.label"
                  :value="item.value" 
                  />
                </el-select>
            </el-form-item>
          </el-col>
        </el-row>


          <div v-if="form.doorCount === 2">
            <el-row>
              <el-col :span="12">
                <el-form-item label="柜门方向(左门)" prop="doorDirectionLeft">
                  <el-select 
                      v-model="form.doorDirectionLeft" 
                      placeholder="请选择开门方向" 
                      clearable
                      style="width:240px"
                    >
                      <el-option 
                      v-for="item in container_openDoor_direction" 
                      :key="item.value" 
                      :label="item.label"
                      :value="item.value" 
                      />
                    </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="柜门方向(右门)" prop="doorDirectionRight">
                  <el-select 
                      v-model="form.doorDirectionRight" 
                      placeholder="请选择开门方向" 
                      clearable
                      style="width:240px"
                    >
                      <el-option 
                      v-for="item in container_openDoor_direction" 
                      :key="item.value" 
                      :label="item.label"
                      :value="item.value" 
                      />
                    </el-select>
                </el-form-item>
              </el-col>
            </el-row>
           
          </div>
          <el-row>
              <el-col :span="12">
            <el-form-item label="总容量" prop="cabinetVolume">
              <el-input v-model="form.cabinetVolume" placeholder="请输入总容量" type="number" > 
                <i slot="suffix"  style="font-style:normal;margin-right: 10px; line-height: 30px; color: #1e1e1e">L</i>
              </el-input>
            </el-form-item>
          </el-col>
            </el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入" maxlength="40" show-word-limit/>
            </el-form-item>
          </el-col>
        
      </el-form>        

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>

    </el-dialog>
  </div>
</template>

<script>

import { listContainerType, getContainerTypeDetail, addContainerType, updateContainerType, exportContainerType, delContainerType } from "@/api/container/containerType";

export default {
  name: "containerType",
  data() {
    return {
      goldenTier:[],
      // 选中的黄金层数
      goldLayerList: [],
      // 表单数据
      form : {
        address: null,
        classify:null,
        code: null,
        direction: null,
        model: null,
        name: null,
        paimianAngle: null,
        port: null,
        type: 2,
        goldenTier: [],
        cabinetVolume:null,
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中项目名数组
      names: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目表格数据
      containerTypelList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn:'id',
        isAsc:'desc'
      },
      // 开门方向
      container_openDoor_direction: [
        {label:'顺时针',value:1},
        {label:'逆时针',value:2}       
      ],
      // 柜子作用类型
      container_type:[
        {label:'冷柜',value:0},
        {label:'热柜',value:1},
      ],
      // 柜门数量
      container_door_count:[
        {label:'1',value:1},
        {label:'2',value:2},
      ],
      // 表单校验
      rules: {
        name: [
          { required: true, message: "冰柜类型名称不能为空", trigger: "blur" }
        ],
        layerCount: [
          { required: true, message: "层数不能为空", trigger: "blur" }
        ],
        coldHotType: [
          { required: true, message: "请选择冷热类型", trigger: "blur" }
        ],
        displayVolume: [
          { required: true, message: "请填写陈列容积", trigger: "blur" }
        ],
        doorCount: [
          { required: true, message: "柜门数量不能为空", trigger: "blur" }
        ],
        doorDirectionLeft: [
          { required: true, message: "请选择柜门方向", trigger: "blur" }
        ],
        cabinetVolume: [
          { required: true, message: "请填写总容量", trigger: "blur" }
      ],
      }
    };
  },
  watch:{
    'form.layerCount': {
      handler(val) {
        if (val) {
          // 初始化
          this.goldLayerList = [];
          let temp
          for(var i = 1; i <= val; i++){
            temp = {
              value:i,
              label:i.toString()
            }
            this.goldLayerList.push(temp)
          }
          console.log("生成的列表为",this.goldLayerList)
          this.$forceUpdate();
        }
      },
    }
  },
  created() {
    this.getContainerTypeList();
  },
  methods: {
    /** 黄金层点击触发 */
    handleGoldenLayerChange() {
      //排序一下
      this.goldenTier.sort()
    },
    /** 获取冰柜类型列表 */
    getContainerTypeList(){
      this.loading = true;
      listContainerType(this.queryParams).then((res) =>{
        if(res.code === 200){
          this.containerTypelList = res.rows
          this.total = res.total
        }
      }).catch((err) =>{
        this.$message.error("冰柜类型列表获取失败，原因："+err.message)
      }).finally(() =>{
        this.loading = false;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.goldLayerList= [
        {value: 1, label: '1'},
        {value: 2, label: '2'},
        {value: 3, label: '3'},
        {value: 4, label: '4'},
        {value: 5, label: '5'},
        {value: 6, label: '6'}
      ],
      this.form = {}
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getContainerTypeList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.names = selection.map(item => item.model)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加冰柜类型";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getContainerTypeDetail(id).then(response => {
        this.form = response.data;
        if (this.form && this.form.goldenTier) {
          this.goldenTier = this.form.goldenTier.split(',');
          this.goldenTier.pop();
        }
        this.form.coldHotType = parseInt(this.form.coldHotType)
        this.form.doorCount = parseInt(this.form.doorCount)
        this.form.doorDirectionLeft = parseInt(this.form.doorDirectionLeft)
        this.form.doorDirectionRight = parseInt(this.form.doorDirectionRight)
        this.open = true;
        this.title = "修改冰柜类型信息";
      });
    },
    /** 提交按钮 */
    submitForm(){
      this.$refs["form"].validate(valid => {
        if (valid) {
          // 校验
          if(this.form.doorCount === 2 && !this.form.doorDirectionRight ){
            this.$message.warning("当柜门数量为2时，请选择柜门方向(右)");
            return;
          }
          if(this.goldenTier.length === 0){
            this.$message.warning("请选择黄金层数");
            return;
          }
          // 数据处理
          this.form.goldenTier = this.goldenTier;
          let goldenTierString = ''
          if (this.form.goldenTier) {
            goldenTierString = ((this.form.goldenTier.length > 1) ? ( this.form.goldenTier.join(',') + ',') : (this.form.goldenTier[0] + ','));
          }
          let params = {
            name: this.form.name,
            layerCount: this.form.layerCount,
            coldHotType: parseInt(this.form.coldHotType),
            displayVolume: this.form.displayVolume,
            doorCount: this.form.doorCount,
            doorDirectionLeft: this.form.doorDirectionLeft.toString(),
            doorDirectionRight: this.form.doorDirectionRight ? this.form.doorDirectionRight.toString() : null,
            goldenTier: goldenTierString,
            remark: this.form.remark,
            id:this.form.id ? this.form.id : null,
            cabinetVolume:this.form.cabinetVolume ? this.form.cabinetVolume : null,
          }
          // 接口调用
          if (this.form.id != null) {
            updateContainerType(params).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getContainerTypeList();
            });
          } else {
            addContainerType(params).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getContainerTypeList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      //todos 修改
      const ids = row.id || this.ids;
      const names = row.name || this.names
      this.$modal.confirm('是否确认删除冰柜类型名称名为"' + names + '"的数据项？').then(function() {
        return delContainerType(ids);
      }).then(() => {
        this.getContainerTypeList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('/genius/platform-container-type/export', {
        ...this.queryParams
      }, `containerTypelList_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss">
.formTitle{
  width:100%;
  text-align:center;
  margin:15px 0 15px 0;
  font-weight:border;
  font-size:20px
}
</style>
